package work.wiwf.web.cfg;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import com.gitee.fastmybatis.core.FastmybatisConfig;
import com.gitee.fastmybatis.core.ext.SqlSessionFactoryBeanExt;
import com.gitee.fastmybatis.core.support.DateFillInsert;
import com.gitee.fastmybatis.core.support.DateFillUpdate;

import work.wiwf.web.cfg.bean.DataSourceDefBean;
import work.wiwf.web.util.DataSourceUtil;

@Configuration
public class FastmybatisDefCfg {

	@Resource(name = DataSourceDefBean.NAME)
	private DataSource dataSource;

	public static final String basePackage = "work.wiwf.web.model.sys.bean.entity";
	/** mybatis的config文件路径 */
	public static final String mybatisConfigLocation = "classpath:mybatis/mybatisConfig.xml";
	/** mybatis的mapper文件路径 */
	public static final String mybatisMapperLocations = "classpath:mybatis/def/*.xml";

	/** 数据源名称 */
	/** sqlSessionTemplate名称 */
	public static final String sqlSessionTemplateName = "sst_" + DataSourceDefBean.NAME;
	/** sqlSessionFactory名称 */
	public static final String sqlSessionFactoryName = "ssf_" + DataSourceDefBean.NAME;
	/** transactionManager名称 */
	public static final String transactionManagerName = "tm_" + DataSourceDefBean.NAME;
	/** transactionTemplate名称 */
	public static final String transactionTemplateName = "tt_" + DataSourceDefBean.NAME;
	/* **************************************************** */
	
	private FastmybatisConfig fastmybatisConfig() {
		FastmybatisConfig config = new FastmybatisConfig();
		
		config.setCamel2underline(true);
		config.setFills(Arrays.asList(
				new DateFillInsert(DataSourceUtil.DbInsertDateColumnName), 
				new DateFillUpdate(DataSourceUtil.DbUpdateDateColumnName)));
		
		return config;
	}

	@Primary
	@Bean(name = sqlSessionFactoryName)
	SqlSessionFactory sqlSessionFactory() throws Exception {
		System.out.println(dataSource.getConnection());

		SqlSessionFactoryBeanExt bean = new SqlSessionFactoryBeanExt();

		bean.setDataSource(dataSource);
		bean.setConfigLocation(DataSourceUtil.getResource(mybatisConfigLocation));
		bean.setMapperLocations(DataSourceUtil.getResources(mybatisMapperLocations));
		
		bean.setBasePackage(basePackage);
		bean.setConfig(fastmybatisConfig());

		return bean.getObject();
	}

	@Primary
	@Bean(name = sqlSessionTemplateName)
	SqlSessionTemplate sqlSessionTemplate(
			@Autowired @Qualifier(sqlSessionFactoryName) SqlSessionFactory sessionFactory) throws Exception {
		SqlSessionTemplate template = new SqlSessionTemplate(sessionFactory); // 使用上面配置的Factory
		return template;
	}

	@Primary
	@Bean(name = transactionManagerName)
	PlatformTransactionManager annotationDrivenTransactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}

	@Primary
	@Bean(name = transactionTemplateName)
	TransactionTemplate transactionTemplate(
			@Autowired @Qualifier(transactionManagerName) PlatformTransactionManager transactionManager) {
		return new TransactionTemplate(transactionManager);
	}

}
